import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';

//自动上架Provider
class AutoMountingProvider extends ChangeNotifier {
  
  // _netStatus系列表示请求状态：
  // false未请求
  // true请求中

  bool _netStatus1 = false;
  List _listData = List(); //列表数据

  bool _netStatus2 = false;
  List _detailListData = List(); //详情列表数据

  String _inPutawaytypename=""; //上架类型
  List _inautoputawayList = List(); //上架类型列表

  Map<String,dynamic> _detailData=Map(); //详情数据

  String _orderId;

  bool _netStatus3 = false;
  List _allotLocation = List(); //选择货位列表
  
  bool _netStatus4 = false; //取货下架

  get netStatus1=>_netStatus1;
  get netStatus2=>_netStatus2;
  get netStatus3=>_netStatus3;
  get netStatus4=>_netStatus4;

  set setNetStatus4(bool value) {
    _netStatus4 = value;
    notifyListeners();
  }
  
  //getter方法获取数据
  get listData=>_listData;

  get orderId=>_orderId;

  get detailListData=>_detailListData;

  get inPutawaytypename=>_inPutawaytypename;
  get inautoputawayList=>_inautoputawayList;

  get detailData=>_detailData;

  get allotLocation=>_allotLocation;

  //列表查询
  void getList({Map<String,dynamic> sendObj}) async {
    sendObj["warehouseid"] = DataSingleton().warehouseChoice.id;
    //初始化数据和加载状态
    _netStatus1=true;
    _listData=[];
    notifyListeners();
    // print(DataSingleton().warehouseChoice.id);
    Map<String,dynamic> res = await Internet().get("WMS/MiWarehouseInOrder/GetOrderAutoPutawayList",params:sendObj);
    if(res['flag'] == "success"){
      _listData=res['datas'];
    }else{
      TostUtil.showToast(res['msg']);
    }
    _netStatus1=false;
    notifyListeners();
  }

  //详情列表查询分类别
  void getDetailListData({@required String orderid}) async {
    Map<String,dynamic> sendObj={
      "orderid":orderid,
      "pageNo":0
    };
    //初始化数据和加载状态
    _netStatus2=true;
    _detailListData=[];
    notifyListeners();
    print("自动上架详情~~~~~~~~~~~~~~~~");
    Map<String,dynamic> res = await Internet().get("WMS/MiWarehouseInOrder/GetOrderDetailAutoPutaway",params:sendObj);
    if(res['flag'] == "success"){
      _detailListData=res['datas'];
    }else{
      TostUtil.showToast(res['msg']);
    }
    _netStatus2=false;
    notifyListeners();
  }

  //列表详情分类名称
  void setInPutawaytypename(String name){
    this._inPutawaytypename=name;
    notifyListeners();
  }
  //列表详情分类列表
  void setInautoputawayList(List datas){
    this._inautoputawayList=datas;
    notifyListeners();
  }

  //存储任务id
  void setOrderId(String id){
    this._orderId=id;
  }

  //货位列表查询
  void getAllotLocationData() async {
    Map<String,dynamic> sendObj={
      "orderid":_detailData['orderid'],
      "materialid":_detailData['materialid'],
      "orderdispatchdetailid":_detailData['orderdispatchdetailid'],
      "orderdetailid":_detailData['orderdetailid'],
      "warehouseid": DataSingleton().warehouseChoice.id
    };
    //初始化数据和加载状态
    _netStatus3=true;
    _allotLocation=[];
    notifyListeners();
    Map<String,dynamic> res = await Internet().get("WMS/MiWarehouseInOrder/ArtificialAllotLocation",params:sendObj);
    if(res['flag'] == "success"){
      _allotLocation=res['datas'];
    }else{
      TostUtil.showToast(res['msg']);
    }
    _netStatus3=false;
    notifyListeners();
  }

  //详情数据
  void setDetailData({@required Map<String,dynamic> obj}) async {
    _detailData=obj;
    notifyListeners();
  }
  
  //人工指定货位——上架
  void sendOrderAutoPutqwayTask({Map<String,dynamic> sendObj,Function callbackFuns}) async {
    sendObj["warehouseid"] = DataSingleton().warehouseChoice.id;
    _netStatus4=true;
    notifyListeners();
    Map<String,dynamic> res = await Internet().post("WMS/MiWarehouseInOrder/SendOrderAutoPutqwayTask",params:sendObj);
    callbackFuns(res);
    _netStatus4=false;
    notifyListeners();
  }
  
  //人工指定货位——下架
  void sendOrderTakeMaterialTask({Map<String,dynamic> sendObj,Function callbackFuns}) async {
    sendObj["warehouseid"] = DataSingleton().warehouseChoice.id;
    _netStatus4=true;
    notifyListeners();
    print("下架请求sendObj~~~~~~~~~~~~~~");
    print(sendObj);
    Map<String,dynamic> res = await Internet().post("WMS/MiWarehouseInOrder/SendOrderTakeMaterialTask",params:sendObj);
    print("下架请求~~~~~~~~~~~~~~");
    print(res);
    _netStatus4=false;
    callbackFuns(res);
    notifyListeners();
  }

}
